package com.dao.impl;

import com.Bean.Clientroom;
import com.dao.ClientRoomDao;
import com.util.JdbcUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClientRoomDaoimpl implements ClientRoomDao {

    @Override
    public int insert(Clientroom clientroom) throws SQLException {
       return 0;
    }

    @Override
    public List<Clientroom> queryData(Clientroom clientroom) throws IllegalAccessException, SQLException, InstantiationException {

//        String sql = "select s.lid,s.pname,s.pphone,s.ptn,s.ptotal,s.pcount,s.lasttime,s.pcretime,m.rname,s.intime from \n" +
//                "(select room_id.rid,room.rname from room join room_id on room.rtid=room_id.rtid) m\n" +
//                "join \n" +
//                "(select room_list.intime,room_list.lid,room_list.rid,s.pname,s.pphone,s.ptypename ptn,s.ptotal,s.pcount,s.lasttime,s.pcretime from room_list Join (select client.pno,client.pname,client.pphone,clienttype.pname ptypename,client.ptotal,client.pcount,client.lasttime,client.pcretime from client JOIN clienttype ON client.cli_pid=clienttype.pid and client.pname !='null') s on room_list.pno=s.pno) s on s.rid=m.rid";
        String sql ="select  client.pid,client.pname pname,clienttype.pname ptyname,pphone,ptotal,pcount,lasttime,pcretime,pbrithday,pgender " +
                " from client,clienttype where client.cli_pid=clienttype.pid and pphone !='' and client.pname != ''";

        ArrayList values = new ArrayList();

        if (clientroom!=null) {

            if (clientroom.getPphone() != null && !"".equals(clientroom.getPphone())) {
                sql += " and pphone = ? ";
                values.add(clientroom.getPphone());
            }else if (clientroom.getPname() != null && !"".equals(clientroom.getPname())) {
                sql += " and client.pname like ? ";
                values.add("%"+clientroom.getPname()+"%");
            }
            if(clientroom.getBeginTime() != null && !"".equals(clientroom.getBeginTime()) && clientroom.getEndTime() != null && !"".equals(clientroom.getEndTime())){
                sql += " and lasttime BETWEEN ? and ?";
                values.add(clientroom.getBeginTime());
                values.add(clientroom.getEndTime());
            }
            if (clientroom.getPtn() != null && !"".equals(clientroom.getPtn())) {
                sql += " and  clienttype.pname = ?";
                values.add(clientroom.getPtn());
            }

        }
        List<Clientroom> clientroomList = JdbcUtil.queryData(sql, Clientroom.class, values.toArray());
        return clientroomList;
    }

    @Override
    public int update(Clientroom clientroom) throws SQLException {
        return 0;
    }

    @Override
    public int deleteById(Object id) throws SQLException {
        return 0;
    }
}
